﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

using System.Web;

namespace DotNetNuke.Modules.ActiveForums
{
#region RewardInfo
	public class RewardInfo
	{
		private int _RankId;
		private int _PortalId;
		private int _ModuleId;
		private string _RankName;
		private int _MinPosts;
		private int _MaxPosts;
		private string _Display;
		public int RankId
		{
			get
			{
				return _RankId;
			}
			set
			{
				_RankId = value;
			}
		}
		public int PortalId
		{
			get
			{
				return _PortalId;
			}
			set
			{
				_PortalId = value;
			}
		}
		public int ModuleId
		{
			get
			{
				return _ModuleId;
			}
			set
			{
				_ModuleId = value;
			}
		}
		public string RankName
		{
			get
			{
				return _RankName;
			}
			set
			{
				_RankName = value;
			}
		}
		public int MinPosts
		{
			get
			{
				return _MinPosts;
			}
			set
			{
				_MinPosts = value;
			}
		}
		public int MaxPosts
		{
			get
			{
				return _MaxPosts;
			}
			set
			{
				_MaxPosts = value;
			}
		}
		public string Display
		{
			get
			{
				return _Display;
			}
			set
			{
				_Display = value;
			}
		}

	}
#endregion
#region RewardController
	public class RewardController
	{
		public RewardInfo Reward_Save(RewardInfo reward)
		{
			int rankId = DataProvider.Instance().Ranks_Save(reward.PortalId, reward.ModuleId, reward.RankId, reward.RankName, reward.MinPosts, reward.MaxPosts, reward.Display);
			return Reward_Get(reward.PortalId, reward.ModuleId, rankId);
		}
		public void Reward_Delete(int PortalId, int ModuleId, int RankId)
		{
			DataProvider.Instance().Ranks_Delete(PortalId, ModuleId, RankId);
		}
		public RewardInfo Reward_Get(int PortalId, int ModuleID, int RankId)
		{
			RewardInfo ri = new RewardInfo();
			IDataReader dr = DataProvider.Instance().Ranks_Get(PortalId, ModuleID, RankId);
			while (dr.Read())
			{
				ri.Display = dr["Display"].ToString();
				ri.MaxPosts = Convert.ToInt32(dr["MaxPosts"]);
				ri.MinPosts = Convert.ToInt32(dr["MinPosts"]);
				ri.ModuleId = ModuleID;
				ri.PortalId = PortalId;
				ri.RankId = Convert.ToInt32(dr["RankId"]);
				ri.RankName = Convert.ToString(dr["RankName"]);
			}
			dr.Close();
			return ri;
		}
		public List<RewardInfo> Reward_List(int PortalId, int ModuleId, bool UseCache)
		{
			string cacheKey = string.Format(CacheKeys.Rewards, ModuleId.ToString());
			List<RewardInfo> rl = new List<RewardInfo>();
			if (UseCache)
			{
				rl = (List<RewardInfo>)(DataCache.CacheRetrieve(cacheKey));
				if (rl == null)
				{
					rl = Reward_List(PortalId, ModuleId);
					DataCache.CacheStore(cacheKey, rl);
				}
			}
			else
			{
				rl = Reward_List(PortalId, ModuleId);
			}
			return rl;
		}
		public List<RewardInfo> Reward_List(int PortalId, int ModuleId)
		{
			List<RewardInfo> rl = new List<RewardInfo>();
			IDataReader dr = DataProvider.Instance().Ranks_List(PortalId, ModuleId);
			dr.Read();
			dr.NextResult();
			while (dr.Read())
			{
				RewardInfo ri = new RewardInfo();
				ri.Display = Convert.ToString(dr["Display"]);
				ri.MaxPosts = Convert.ToInt32(dr["MaxPosts"]);
				ri.MinPosts = Convert.ToInt32(dr["MinPosts"]);
				ri.ModuleId = ModuleId;
				ri.PortalId = PortalId;
				ri.RankId = Convert.ToInt32(dr["RankId"]);
				ri.RankName = Convert.ToString(dr["RankName"]);
				rl.Add(ri);
			}
			dr.Close();
			return rl;
		}
	}
#endregion


}
